package com.example.snackbar.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.snackbar.Message;
import com.example.snackbar.R;

import java.util.ArrayList;

public class selfListViewAdapter extends ArrayAdapter {

    private int resource;
    public selfListViewAdapter(@NonNull Context context, int resource, ArrayList<Message> list) {
        super(context, resource,list);
        this.resource = resource;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
//        Message message = (Message) getItem(position);
//        View view = LayoutInflater.from(getContext()).inflate(resource,parent,false);
//        ImageView imageView = view.findViewById(R.id.imageView);
//        TextView textView = view.findViewById(R.id.textView);
//        imageView.setBackgroundResource(message.getIcon());
//        textView.setText(message.getMessage());
        Message message = (Message) getItem(position);
        ViewHolder viewHolder = null;
        View view;
        if (convertView==null){
            view = LayoutInflater.from(getContext()).inflate(resource,parent,false);
            viewHolder = new ViewHolder();
            viewHolder.imageView = view.findViewById(R.id.imageView);
            viewHolder.textView = view.findViewById(R.id.textView);
            view.setTag(viewHolder);
        }else {
            view =convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.imageView.setBackgroundResource(message.getIcon());
        viewHolder.textView.setText(message.getMessage());
        return view;
    }
    class ViewHolder{
        ImageView imageView;
        TextView textView;
    }
}
